# The vms for control plane nodes
resource "libvirt_domain" "cpnodes" {
  count = var.cp_node_count

  name      = format("${var.cluster_name}-cp%02d", count.index + 1)
  vcpu      = var.cp_vcpu
  memory    = var.cp_memory
  autostart = true
  cloudinit = element(libvirt_cloudinit_disk.cpnode-init[*].id, count.index)

  #  cpu {
  #    mode = "host-model"
  #  }

  boot_device {
    dev = ["hd"]
  }

  disk {
    volume_id = element(libvirt_volume.cp-osdisk[*].id, count.index)
    scsi      = true
  }

  network_interface {
    bridge   = var.network_bridge
    hostname = format("${var.cluster_name}-cp%02d", count.index + 1)
  }

  console {
    type        = "pty"
    target_type = "serial"
    target_port = "0"
  }

}

# The vms for work nodes
resource "libvirt_domain" "worknodes" {
  count = var.work_node_count

  name      = format("${var.cluster_name}-work%02d", count.index + 1)
  vcpu      = var.work_vcpu
  memory    = var.work_memory
  autostart = true
  cloudinit = element(libvirt_cloudinit_disk.worknode-init[*].id, count.index)

  #  cpu {
  #    mode = "host-model"
  #  }

  boot_device {
    dev = ["hd"]
  }

  disk {
    volume_id = element(libvirt_volume.work-osdisk[*].id, count.index)
    scsi      = true
  }

  disk {
    volume_id = element(libvirt_volume.work-datadisk[*].id, count.index)
  }

  network_interface {
    bridge   = var.network_bridge
    hostname = format("${var.cluster_name}-work%02d", count.index + 1)
  }

  console {
    type        = "pty"
    target_type = "serial"
    target_port = "0"
  }

}

output "cpnode-vms" {
  value = {
    for i in range(var.cp_node_count) :
    libvirt_domain.cpnodes[i].name => var.cp_ipv4_addresses[i]
  }
}

output "worknode-vms" {
  value = {
    for i in range(var.work_node_count) :
    libvirt_domain.worknodes[i].name => var.work_ipv4_addresses[i]
  }
}
